package com.wn.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn.dao.*;
import com.wn.entity.Guideinfo;
import com.wn.entity.Privatemade;
import com.wn.entity.Requirement;
import com.wn.entity.Travelinfo;
import com.wn.service.PrivatemadeService;
import com.wn.service.RoutesService;
import com.wn.utils.PageParam;
import com.wn.utils.Rsp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * (Privatemade)表服务实现类
 *私人方案定制
 * @author makejava
 * @since 2020-10-26 09:32:42
 */
@Service("privatemadeService")
@Transactional(rollbackFor = Exception.class)
public class PrivatemadeServiceImpl implements PrivatemadeService {
    @Resource
    private PrivatemadeDao privatemadeDao;
    @Autowired
    private RequirementDao requirementDao;
    @Autowired
    private GuideinfoDao guideinfoDao;
    @Autowired
    private TravelinfoDao travelinfoDao;
    @Autowired
    private RoutesService routesService;


    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Privatemade queryById(String id) {
        return this.privatemadeDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
//     * @param offset 查询起始位置
//     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public PageInfo<Privatemade> queryAllByLimit(PageParam<Privatemade> pageParam) {

        if(!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())){
            //执行高级检索
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for(int i=0;i<pageParam.getOrderParams().length;i++){
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }


            List<Privatemade> privatemadeList=privatemadeDao.superSearch(pageParam.getSuperSearchKeyWord());
            PageInfo<Privatemade> privatemadePageInfo = new PageInfo<Privatemade>(privatemadeList);

            return privatemadePageInfo;

        }else{
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for(int i=0;i<pageParam.getOrderParams().length;i++){
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }


            List<Privatemade> privatemadeList=privatemadeDao.searchSomeByPage(pageParam.getModel());
            PageInfo<Privatemade> privatemadePageInfo = new PageInfo<Privatemade>(privatemadeList);

            return privatemadePageInfo;

        }
    }

    /**
     * 新增数据
     *
     * @param privatemade 实例对象
     * @return 实例对象
     */

    @Override
    public Rsp insert(Privatemade privatemade) {

        privatemade.setId(UUID.randomUUID().toString().replaceAll("-", "")+"_plan");
        privatemade.setCreatetime(new Date());

        return privatemadeDao.insert(privatemade)==1?Rsp.success("添加成功"):Rsp.fail().msg("添加失败");
    }

    /**
     * 修改数据
     *
     * @param privatemade 实例对象
     * @return 实例对象
     */
    @Override
    public Rsp update(Privatemade privatemade) {

        return privatemadeDao.update(privatemade)==1?Rsp.success(null).msg("修改成功"):Rsp.fail().msg("修改失败");
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.privatemadeDao.deleteById(id) > 0;
    }


    @Override
    public Rsp deleteLogical(Privatemade privatemade) {

        privatemade.setIsdeleted(1);

        return privatemadeDao.update(privatemade)==1?Rsp.success(null).msg("逻辑删除成功"):Rsp.fail().msg("逻辑删除失败");
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> findDetailById(String id) {
        System.out.println(id+"***********************");
        Map<String, Object> data = new HashMap<>();
        String type = id.split("_")[1];
        if (type.equals("req")){
            Requirement requirement = requirementDao.queryById(id);
            data.put("requirement",requirement);
            return data;
        }
        Privatemade privatemade = privatemadeDao.queryById(id);
        Map<String, Object> map = routesService.queryById(privatemade.getRoutesid());
        Travelinfo travelinfo = travelinfoDao.queryById(privatemade.getTransportation());
        Guideinfo guideinfo = guideinfoDao.queryById(privatemade.getGuideid());
        map.put("privatemade",privatemade);
        map.put("travelinfo",travelinfo);
        map.put("guideinfo",guideinfo);
        return map;
    }
}